// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: COM error handling for FormsPlayer project.
//   $Id$



#include "stdafx.h"
#include "FP_ReturnCodes.h"
#include "XFormsApp.h"

extern CXFormsApp theApp;

HRESULT SetupErrorInfo(HRESULT errcode,const wchar_t * psParam)
{
	return theApp.FormsManager().GetErrorSource().SetupErrorInfo(errcode,psParam);
}

HRESULT CErrorInfoHelper::GetMessageFormatString(HRESULT code,CComBSTR & sFormatMessage)
{
	CStringW sXPath;
	sXPath.Format(L"/errors/message[@errorcode='0x%x']",code);
	BSTR bsXPath = sXPath.AllocSysString();
	
	CComPtr<IXMLDOMNode> pMessageNode = 0;
	HRESULT hr = m_pXMLParser->selectSingleNode(bsXPath,&pMessageNode);
	if(pMessageNode)
		pMessageNode->get_text(&sFormatMessage);
	::SysFreeString(bsXPath);
	return hr;
}

HRESULT CErrorInfoHelper::SetupErrorInfo(HRESULT errcode,...)
{
	// Create an error info object
	CComPtr<ICreateErrorInfo> pCreateErrorInfo;
	HRESULT hr = CreateErrorInfo(&pCreateErrorInfo);

	if(SUCCEEDED(hr)) 
	{
		//Fish the appropriate Format String out of the config file.
		CComBSTR sFormatMessage;
		GetMessageFormatString(errcode,sFormatMessage);

		//Format the message with the given arguments.
		BSTR sDescription = 0;
		if(::isEmpty(sFormatMessage))
		{
			//If there was no format string, 
			//	give the default message.
			sDescription = ::SysAllocString(L"An unexpected error has occurred inside FormsPlayer.");
		}
		else
		{
			CStringW sMessage;
			va_list args;
			va_start(args, errcode);
			sMessage.FormatV(sFormatMessage, args);
			va_end(args);
			sDescription = sMessage.AllocSysString();		
		}

		// Setup the error info object
		hr = pCreateErrorInfo->SetSource(m_sSource);
		if(SUCCEEDED(hr)) 
		{
			hr = pCreateErrorInfo->SetDescription(sDescription);
			if(SUCCEEDED(hr)) 
			{
				CComQIPtr<IErrorInfo> pErrorInfo = pCreateErrorInfo;
				if(pErrorInfo)
					hr = SetErrorInfo(0, pErrorInfo);
			}
		}
		::SysFreeString(sDescription);
	}

	return hr;
}


CErrorInfoHelper::CErrorInfoHelper(BSTR sSource)
	:m_sSource(sSource)
{
	char fullpath[MAX_PATH];
	::GetModuleFileName(_Module.m_hInst,fullpath,MAX_PATH);
	
	//This is not strictly reliable, must find a way to get the full path
	//	not the shortened name.
	char * fileName = strrchr(fullpath,'\\');
	
	//increment filename to after the directory separator
 	++fileName;
	strcpy(fileName,"errors.xml");
	CComBSTR bsFileName(fullpath);
	
	HRESULT hr = m_pXMLParser.CoCreateInstance(CLSID_FreeThreadedDOMDocument60);
	
	if(m_pXMLParser && SUCCEEDED(hr))
	{
		VARIANT_BOOL bIsSuccess;
		m_pXMLParser->put_async(false);
		m_pXMLParser->load(CComVariant(bsFileName), &bIsSuccess);
	}
}

CErrorInfoHelper::~CErrorInfoHelper()
{
}

